using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace DarkLordsApprenticeV2
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Hero : DrawableGameComponent
    {
        public Hero(Game game, Vector2 pos, float atk, float initSpeed, float initHP)
            : base(game)
        {
            index = 0;
            animCounter = 0;
            frames = new Texture2D[4][];
            frames[0] = new Texture2D[2];
            frames[1] = new Texture2D[2];
            frames[2] = new Texture2D[2];
            frames[3] = new Texture2D[2];
            Position = pos;
            scale = Position.Y / 600;
            state = State.Front;
            rotation = 0;
            attack = atk;
            speed = initSpeed;
            hp = initHP;
            flyVelocity = new Vector2(0, 0);
        }

        int index;
        int animCounter;
        Texture2D[][] frames;
        Texture2D image;
        SpriteBatch spriteBatch;
        public Vector2 Position;
        public float scale;
        float rotation;

        enum State { Front, Side, ToastyFront, ToastySide };
        State state;

        float attack;
        float speed;
        float hp;
        Vector2 flyVelocity;
        Boolean dead = false;

        protected override void LoadContent()
        {
            frames[0][0] = Game.Content.Load<Texture2D>("HeroWalking1");
            frames[0][1] = Game.Content.Load<Texture2D>("HeroWalking2");
            frames[1][0] = Game.Content.Load<Texture2D>("HeroSide1");
            frames[1][1] = Game.Content.Load<Texture2D>("HeroSide2");
            frames[2][0] = Game.Content.Load<Texture2D>("HeroBurnt");
            frames[2][1] = Game.Content.Load<Texture2D>("HeroAsh");
            frames[3][0] = Game.Content.Load<Texture2D>("HeroBurntSide");
            frames[3][1] = Game.Content.Load<Texture2D>("HeroAsh");

            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            switch (state)
            {
                case State.Front:
                    image = frames[0][0];
                    break;

                case State.Side:
                    image = frames[1][0];
                    break;

                case State.ToastyFront:
                    image = frames[2][0];
                    break;

                case State.ToastySide:
                    image = frames[2][0];
                    break;
            }
        }

        public override void Update(GameTime gameTime)
        {
            int stateIndex = 0;
            int mod = 0;
            switch (state)
            {
                case State.Front:
                    stateIndex = 0;
                    mod = 20;
                    break;

                case State.Side:
                    stateIndex = 1;
                    mod = 20;
                    break;

                case State.ToastyFront:
                    stateIndex = 2;
                    mod = 60;
                    break;

                case State.ToastySide:
                    stateIndex = 3;
                    mod = 60;
                    break;
            }

            if (animCounter % mod == 0)
            {
                index++;
                if (index >= frames[stateIndex].Length)
                {
                    index = 0;
                }
                image = frames[stateIndex][index];

            }

            animCounter++;

            if (state == State.Front || state == State.Side)
            {
                if (Position.Y < 0 || Position.Y > 900 || Position.X < -100 || Position.X > 900)
                {
                    dead = true;
                }
                else if (hp <= 0 && (flyVelocity.X != 0 || flyVelocity.Y != 0))
                {
                    Position.X += flyVelocity.X;
                    Position.Y += flyVelocity.Y;
                    flyVelocity.Y += 1;
                    if (flyVelocity.X < 0)
                    {
                        rotation -= 1;
                    }
                    else
                    {
                        rotation += 1;
                    }
                }
                else
                {
                    if (Position.Y < 450)
                    {
                        Position.Y += speed;
                    }
                    else
                    {
                        if (Position.Y < 500)
                        {
                            SwitchToSide();
                            Position.X = 150;
                            Position.Y = 800;
                        }
                        else if (Position.Y > 500 && Position.Y < 590)
                        {
                            Position.X += speed;
                        }
                        else
                        {
                            Position.Y -= speed;
                        }
                    }
                }
            }
            else
            {
                if (index == 1 && animCounter % mod > 50)
                {
                    dead = true;
                }
            }
            scale = Position.Y / 590;
            if (scale > 1)
            {
                scale = 1;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(image, Position, null, Color.White, rotation, new Vector2(image.Width / 2, image.Height), scale, SpriteEffects.None, 0);
            spriteBatch.End();
        }

        public void SwitchToFront()
        {
            state = State.Front;
            animCounter = 0;
            index = 0;
        }

        public void SwitchToSide()
        {
            state = State.Side;
            animCounter = 0;
            index = 0;
        }

        public void SwitchToToasty()
        {
            if (state == State.Front)
            {
                state = State.ToastyFront;
                image = frames[2][0];
            }
            else if (state == State.Side)
            {
                state = State.ToastySide;
                image = frames[2][0];
            }
            animCounter = 1;
            index = 0;
        }

        public float GetAttack()
        {
            return attack;
        }

        public float GetHP()
        {
            return hp;
        }

        public void LoseHP(float loss)
        {
            hp = hp - loss;
        }

        public float GetWidth()
        {
            return image.Width * scale;
        }

        public void sendFlying(Vector2 flyVel)
        {
            if (hp <= 0)
            {
                flyVelocity = flyVel;
            }
        }

        public Boolean IsDead()
        {
            return dead;
        }
    }
}
